from akamu.wheezy import WheezyCachingAdapterSetup
from akara import global_config, request, module_config, response
from akamu.config.administration import error_reporting

class requires_authority(object):
    """

    Used with WheezyRepozeWrapper and RepozeWrapper as innermost decorator of
    akara service to indicate that authentication is required and cause
    401 response if there is none.  The first argument is the
    message to send to the client

    @requires_authority('Not authorized')
    def akara_service(): pass

    """
    def __init__(self,
                 message = 'Not authorized',
                 noauth = False,
                 simple = True,
                 email_reporting = True):
        self.message            = message
        self.noauth             = noauth
        self.simple             = simple
        self.email_reporting    = email_reporting

    def __call__(self, func):
        func = error_reporting()(func) if self.email_reporting else func
        def innerHandler(*args, **kwds):
            if 'REMOTE_USER' not in request.environ and not self.noauth:
                if self.simple:
                    response.code = 401
                    return self.message
                else:
                    environ, start_response = args
                    start_response("401 Unauthorized",
                        [("Content-Length",  len(self.message))]
                    )
                    return self.message
            else:
                if '_' in kwds:
                    del kwds['_']
                return func(*args, **kwds)
        return innerHandler